<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use Think\Exception;
use User\Api\UserApi;

/**
 * 后台首页控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class PublicController extends \Think\Controller {

    /**
     * 后台用户登录
     * @author 麦当苗儿 <zuojiazi@vip.qq.com>
     */
    public function login($username = null, $password = null, $verify = null){
        if(IS_POST){
            /* 检测验证码 TODO: */
            if(!check_verify($verify)){
                $this->error('验证码输入错误！');
            }

            /* 调用UC登录接口登录 */
            $User = new UserApi;
            $uid = $User->login($username, $password);
            if(0 < $uid){ //UC登录成功
                /* 登录用户 */
                $Member = D('Member');
                if($Member->login($uid)){ //登录用户
                    //TODO:跳转到登录前页面

                    $this->success('登录成功！', U('Index/index'));
                } else {
                    $this->error($Member->getError());
                }

            } else { //登录失败
                switch($uid) {
                    case -1: $error = '用户不存在或被禁用！'; break; //系统级别禁用
                    case -2: $error = '密码错误！'; break;
                    default: $error = '未知错误！'; break; // 0-接口参数错误（调试阶段使用）
                }
                $this->error($error);
            }
        } else {
            if(is_login()){
                $this->redirect('Index/index');
            }else{
                /* 读取数据库中的配置 */
                $config	=	S('DB_CONFIG_DATA');
                if(!$config){
                    $config	=	D('Config')->lists();
                    S('DB_CONFIG_DATA',$config);
                }
                C($config); //添加配置
                
                $this->display();
            }
        }
    }

    /* 退出登录 */
    public function logout(){
        if(is_login()){
            D('Member')->logout();
            session('[destroy]');
            $this->success('退出成功！', U('login'));
        } else {
            $this->redirect('login');
        }
    }

    public function verify(){
        $verify = new \Think\Verify();
        $verify->entry(1);
    }

    public function baidurank(){
        if(IS_CLI){
            if(C('URL_CASE_INSENSITIVE')){
                $_addons        =   ucfirst(parse_name('BaiduRankTool', 1));
                $_controller    =   parse_name('BaiduRankTool',1);
            }

            $TMPL_PARSE_STRING = C('TMPL_PARSE_STRING');
            $TMPL_PARSE_STRING['__ADDONROOT__'] = __ROOT__ . "/Addons/BaiduRankTool";
            C('TMPL_PARSE_STRING', $TMPL_PARSE_STRING);

            $Addons = A("Addons://BaiduRankTool/BaiduRankTool")->searchAll();
        }else{
            echo 'evn wrong!';
        }
    }

    //根据年级id获取班级信息
    public function classInfo($gid=''){

        if(IS_AJAX){
            $list=M('ClassGrade')->field("id,grade_name")->where("gid=%d and status=%d",[$gid,1])->select();
            if($list){
                $this->ajaxReturn($list);
            }

        }
    }

    //根据科目id获取老师数据
    public function teacher_data($sid){

        if(IS_AJAX){
            $where['subject_name']=array('like',"%$sid%");
            $where['status']=1;
            $member=M('Member')->field("uid,nickname")->where($where)->select();
            $this->ajaxReturn($member);
        }
    }
    //判断老师是有空
    public function teacher_busy(){
        if(IS_AJAX){

           $teacherid=I('post.teacherid');//老师id
           $scid=I('post.scid');//课时id
           $sid=I('post.sid');//学期id

           $data=M('ClassSublist')
               ->where("teacher_id=%d and scid=%d and sid=%d  and status=%d",[$teacherid,$scid,$sid,1])
               ->find();

            if($data){
                $arr['status']='error';
                $arr['info']='该老师这个课时已经有课';
                $this->ajaxReturn($arr);
            }else{
               $teacherData=M('TeacherCourse')->where("sid=%d and uid=%d and courses=%d and status=%d",[$sid,$teacherid,$scid,1])->find();
               if($teacherData){
                   $arr['status']='error';
                   $arr['info']='该老师这个这节课没有时间';
                   $this->ajaxReturn($arr);
               }else{
                   $arr['status']='success';
                   $arr['info']='有空';
                   $this->ajaxReturn($arr);
               }
            }
        }
    }




}














